package dataInterpreter;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Image {
	
	private int height;
	private int width;
	private double[][] matrix;
	
	public Image(){
		this.height = 0;
		this.width = 0;
		this.matrix = null;
	}
	
	public double sum(int x, int y, double[][] m){
		if(y == -1){
			return 0;
		}else{
			return (sum(x,y-1,m) + m[x][y]);
		}
	}
	
	public double integral(int x, int y, double[][] m){
		if(x == -1){
			return 0;
		}else{
			return (integral(x-1, y, m) + sum(x, y, m));
		}
	}
	
	public void RGBToGrayScaleImage(String source, String destination){
		Mat im = new Mat();
		Mat im2 = new Mat();
		im = Highgui.imread(source, 1);
		//TODO Confirmar se CV_RGB2GRAY equivale a CV_LOAD_IMAGE_GRAYSCALE
		Imgproc.cvtColor(im, im2, Highgui.CV_LOAD_IMAGE_GRAYSCALE);   //CV_RGB2GRAY
		Highgui.imwrite(destination, im2);  
	}
	
	public void resizeImage(String source, String destination, int newWidth, int newHeight) {
		Mat im = new Mat();
		Mat im2 = new Mat();
		im = Highgui.imread(source, 1);
		Imgproc.resize(im, im2, new Size(newWidth, newHeight));
		Highgui.imwrite(destination, im2);
	}
	
	public void saveImage(String source, String destination, int newWidth,int newHeight) {
		Mat im = new Mat();
		Mat im2 = new Mat();
		Mat im3 = new Mat();
		im = Highgui.imread(source, 1);
		Imgproc.resize(im, im2, new Size(newWidth, newHeight));
		Imgproc.cvtColor(im2, im3, Highgui.CV_LOAD_IMAGE_GRAYSCALE);         //CV_RGB2GRAY
		Highgui.imwrite(destination, im3);
	}
	
	public void pngToJpg(String source, String destination) {
		Mat im = new Mat();
		im = Highgui.imread(source, 1);
		Highgui.imwrite(destination, im);
	}
	
	public void readImage(String path) {

		Mat im = new Mat();
		double[][] inte;

		im = Highgui.imread(path, 0);
		this.width = im.cols();
		this.height	= im.rows();
		inte = new double[this.width][this.width];
		this.matrix = new double [this.width][this.width];
		
		for (int i = 0; i < this.width; i++) {
			this.matrix[i] = new double[height];
			inte[i] = new double[height];
		}
		
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				
				double[] intensity = im.get(i, j);
				inte[i][j] = intensity[0];
			}
		}

		for (int i = 0; i < this.height; i++){
			for (int j = 0; j < this.width; j++){
				matrix[j][i] = integral(j, i, inte);
			}
		}
		matrix[0][0] = matrix [0][0];
	}
	
	// GETs and SETs
	
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public double[][] getMatrix() {
		return matrix;
	}

	public void setMatrix(double[][] matrix) {
		this.matrix = matrix;
	}

}
